<template>
  <div class="app-container">
    <el-form label-width="120px">
      <!-- 讲师头像 -->
      <el-form-item label="banner图">
        <el-upload
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload"
            :action="BASE_API+'/eduoss/fileoss/upload'"
            class="avatar-uploader">
          <img :src="banner.imageUrl" style="height: 200px;width: 300px"/>
        </el-upload>
      </el-form-item>
      <el-form-item label="banner图名称">
        <el-input v-model="banner.title"/>
      </el-form-item>
      <el-form-item label="banner图排序">
        <el-input-number v-model="banner.sort" controls-position="right" :min="0" :max="10"></el-input-number>
      </el-form-item>
      <el-form-item label="banner跳转路径">
        <el-input v-model="banner.linkUrl"/>
      </el-form-item>
      <el-form-item>
        <el-button :disabled="saveBtnDisabled" type="primary"
                   @click="saveOrUpdate">{{ this.buttonInfo }}
        </el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import cms from '@/api/cms';

export default {
  name: 'save',
  data() {
    return {
      banner: {
        title: '',
        sort: 0,
        imageUrl: '',
        linkUrl: ''
      },
      BASE_API: process.env.BASE_API, // 接口API地址
      imagecropperShow: false, // 是否显示上传组件
      imagecropperKey: 0,// 上传组件id
      saveBtnDisabled: false, // 保存按钮是否禁用,
      buttonInfo: '保存'
    }
  },
  created() {
    this.init()
  },
  watch: {
    $route(to, from) {
      this.init()
    }
  },
  methods: {
    handleAvatarSuccess(res, file) {
      this.banner.imageUrl = res.data.url
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/jpeg'
      const isLt2M = file.size / 1024 / 1024 < 2
      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 格式!')
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!')
      }
      return isJPG && isLt2M
    },
    init() {
      if (this.$route.params && this.$route.params.id) {
        const id = this.$route.params.id
        this.buttonInfo = "修改"
        this.getInfo(id)
      } else {
        this.banner = {
          title: '',
          sort: 0,
          imageUrl: '/static/1.jpg',
          linkUrl: ''
        }
      }
    },
    saveOrUpdate() {
      this.saveBtnDisabled = true
      if (!this.banner.id) {
        this.saveData()
      } else {
        this.updateData()
      }
    },
    // 保存
    saveData() {
      cms.addBanner(this.banner).then(response => {
        return this.$message({
          type: 'success',
          message: '保存成功!'
        })
      }).then(resposne => {
        this.$router.push({path: '/cms/list'})
      }).catch((response) => {
// console.log(response)
        this.$message({
          type: 'error',
          message: '保存失败'
        })
      })
    },
    //编辑
    updateData() {
      this.saveBtnDisabled = true
      cms.updateById(this.banner).then(response => {
        return this.$message({
          type: 'success',
          message: '修改成功!'
        })
      }).then(resposne => {
        this.$router.push({path: '/cms/list'})
      }).catch((response) => {
        // console.log(response)
        this.$message({
          type: 'error',
          message: '保存失败'
        })
      })
    },
    //回显数据
    getInfo(id) {
      cms.getBannerInfo(id).then(res => {
        this.banner = res.data.item
      })
    }
  }
}
</script>

<style scoped>

</style>